﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuDada.Core.Post.Domain;
using WuDada.Core.Persistence.Ado;
using WuDada.Core.Persistence;
using NHibernate.Criterion;
using System.Collections;
using WuDada.Core.Generic.Extension;

namespace WuDada.Core.Post.Persistence
{
    public class AnswerDetailDao : AdoDao, IAnswerDetailDao
    {
        public INHibernateDao NHibernateDao { get; set; }        

        /// <summary>
        /// 新增AnswerDetail
        /// </summary>
        /// <param name="answerDetailVO">被新增的AnswerDetail</param>
        /// <returns>新增後的AnswerDetail</returns>
        public AnswerDetailVO CreateAnswerDetail(AnswerDetailVO answerDetailVO)
        {
            NHibernateDao.Insert(answerDetailVO);

            return answerDetailVO;
        }

        /// <summary>
        /// 取得AnswerDetail By AnswerDetailId
        /// </summary>
        /// <param name="answerDetailId">AnswerDetailId</param>
        /// <returns>AnswerDetail</returns>
        public AnswerDetailVO GetAnswerDetailById(int answerDetailId)
        {
            return NHibernateDao.GetVOById<AnswerDetailVO>(answerDetailId);
        }

        /// <summary>
        /// 刪除AnswerDetail
        /// </summary>
        /// <param name="answerDetailVO">被刪除的AnswerDetail</param>
        public void DeleteAnswerDetail(AnswerDetailVO answerDetailVO)
        {
            NHibernateDao.Delete(answerDetailVO);
        }

        /// <summary>
        /// 更新AnswerDetail
        /// </summary>
        /// <param name="answerDetailVO">被更新的AnswerDetail</param>
        /// <returns>更新後的AnswerDetail</returns>
        public AnswerDetailVO UpdateAnswerDetail(AnswerDetailVO answerDetailVO)
        {
            NHibernateDao.Update(answerDetailVO);

            return answerDetailVO;
        }

        /// <summary>
        /// 取得AnswerDetail清單
        /// </summary>
        /// <param name="conditions">搜尋條件</param>
        /// <returns>AnswerDetail清單</returns>
        public IList<AnswerDetailVO> GetAnswerDetailList(IDictionary<string, string> conditions)
        {
            ArrayList param = new ArrayList();
            string fromScript = "select a from AnswerDetailVO a ";
            StringBuilder whereScript = new StringBuilder();
            return this.QueryAnswerDetail(param, fromScript, whereScript, conditions, true).OfType<AnswerDetailVO>().ToList();
        }

        /// <summary>
        /// 取得AnswerDetail數量
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public int GetAnswerDetailCount(IDictionary<string, string> conditions)
        {
            int count = 0;
            ArrayList param = new ArrayList();
            string fromScript = "select count(a.AnswerDetailId) from AnswerDetailVO a ";
            StringBuilder whereScript = new StringBuilder();
            IList result = this.QueryAnswerDetail(param, fromScript, whereScript, conditions, false);
            if (result.Count > 0)
            {
                count = Convert.ToInt32(result[0]);
            }
            return count;
        }

        private IList QueryAnswerDetail(ArrayList param, string fromScript, StringBuilder whereScript, IDictionary<string, string> conditions, bool useOrder)
        {
            AppendAnswerDetailKeyWord(conditions, whereScript, param);
            AppendAnswerDetailAnswer(conditions, whereScript, param);
            AppendAnswerDetailFlag(conditions, whereScript, param);
            AppendAnswerDetailIsCorrect(conditions, whereScript, param);

            string hql = fromScript + "where 1=1 " + whereScript;
            if (useOrder)
            {
                hql += AppendAnswerDetailOrder(conditions);
            }

            return NHibernateDao.Query(hql, param, conditions);
        }

        private void AppendAnswerDetailIsCorrect(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("IsCorrect"))
            {
                whereScript.Append(" and a.IsCorrect = ? ");
                param.Add(bool.Parse(conditions["Flag"]));
            }
        }

        private void AppendAnswerDetailAnswer(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("AnswerId"))
            {
                whereScript.Append(" and a.Answer.AnswerId = ? ");
                param.Add(conditions["AnswerId"]);
            }
        }

        private void AppendAnswerDetailFlag(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("Flag"))
            {
                whereScript.Append(" and a.Flag = ? ");
                param.Add(conditions["Flag"]);
            }
        }

        private void AppendAnswerDetailKeyWord(IDictionary<string, string> conditions, StringBuilder whereScript, ArrayList param)
        {
            if (conditions.IsContainsValue("KeyWord"))
            {
                whereScript.Append(" and (a.PostTitle like ? ) ");
                param.Add("%" + conditions["KeyWord"] + "%");
            }
        }

        private string AppendAnswerDetailOrder(IDictionary<string, string> conditions)
        {
            //// 排序條件
            string order = "order by a.SortNo, a.CreatedDate desc ";
            if (conditions.IsContainsValue("Order"))
            {
                order = conditions["Order"];
            }

            return order;
        }
    }
}
